#include "foudation.hpp"
#include "utils.hpp"

__global__ void pixel_zero(uint8_t *gpu_img)
{
    int x = threadIdx.x + blockIdx.x * blockDim.x;
    int y = threadIdx.y + blockIdx.y * blockDim.y;
    int offset = x + y * blockDim.x * gridDim.x;
    uint8_t Pixel_0 = 0;
    gpu_img[offset] = Pixel_0; 
}

void img_cpu_gpu_wangfan(cv::Mat &img, cv::Mat &img_cpu){
    uint8_t *gpu_img = nullptr;
    int size = img.cols * img.rows * 3 * sizeof(uint8_t);
    int width = img.cols;
    int height = img.rows;
    dim3 block(100, 100);
    dim3 thread(3, 3);
    //GPU开辟空间
    CUDA_CHECK(cudaMalloc(&gpu_img, size));
    //把CPU的图片传到GPU
    CUDA_CHECK(cudaMemcpy(gpu_img, img.data, size, cudaMemcpyHostToDevice));
    pixel_zero<<<block, thread>>>(gpu_img);
    CUDA_CHECK(cudaDeviceSynchronize());
    CUDA_CHECK(cudaMemcpy(img_cpu.data, gpu_img, size, cudaMemcpyDeviceToHost));
    cudaFree(gpu_img);
    // return img_cpu;
}

void experiment_func(std::string file)
{
    cv::Mat img_cpu_source = cv::imread(file);
    // uint8_t *img_cpu_pointer = &img_cpu_source;
    std::cout<<img_cpu_source.type()<<std::endl;
    // std::cout<<typeid( img_cpu_source.at<cv::Vec3d>(0,1) ).name() <<std::endl;

    int width = img_cpu_source.cols;
    int height = img_cpu_source.rows;
    int channels = 3;

    printf("height : %d ,width : %d\n", height, width);
 
    cv::Mat cpu_img (width, height, CV_8UC3);

    // cv::Mat image1 (100, 100, CV_8UC3, 100);
    

    img_cpu_gpu_wangfan(img_cpu_source, cpu_img);
    cv::imwrite("data/zeros_Y.png", cpu_img);
}